package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;
import java.util.stream.Collectors;

public class EditorElement implements PickableBean {

    public static final String FN_INSERTBELOWPATH = "insertBelowPath";
    public static final String FN_DELETEPATH = "deletePath";
    public static final String FN_EDITPATH = "editPath";
    public static final String FN_SEQ = "seq";
    public static final String FN_FIELDS = "fields";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_INSERTBELOWPATH,
            FN_DELETEPATH,
            FN_EDITPATH,
            FN_SEQ,
            FN_FIELDS);

    private String insertBelowPath;
    private String deletePath;
    private String editPath;
    private Long seq;
    private List<ShowValue> fields;

    public EditorElement(String insertBelowPath, String deletePath, String editPath, Long seq, List<ShowValue> fields) {
        setInsertBelowPath(insertBelowPath);
        setDeletePath(deletePath);
        setEditPath(editPath);
        setSeq(seq);
        setFields(fields);
    }

    public EditorElement() {
    }

    public EditorElement duplicate() {
        EditorElement result = new EditorElement();
        result.insertBelowPath = this.insertBelowPath;
        result.deletePath = this.deletePath;
        result.editPath = this.editPath;
        result.seq = this.seq;
        result.fields = this.fields == null ? null : this.fields.stream().map(e4 -> e4 == null ? null : e4.duplicate()).collect(Collectors.toList());
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getInsertBelowPath() {
        return insertBelowPath;
    }

    public void setInsertBelowPath(String value) {
        this.insertBelowPath = value;
    }

    public EditorElement withInsertBelowPath(String value) {
        setInsertBelowPath(value);
        return this;
    }

    public EditorElement withInsertBelowPath(String value, String defaultValue) {
        setInsertBelowPath(value == null ? defaultValue : value);
        return this;
    }

    public String getDeletePath() {
        return deletePath;
    }

    public void setDeletePath(String value) {
        this.deletePath = value;
    }

    public EditorElement withDeletePath(String value) {
        setDeletePath(value);
        return this;
    }

    public EditorElement withDeletePath(String value, String defaultValue) {
        setDeletePath(value == null ? defaultValue : value);
        return this;
    }

    public String getEditPath() {
        return editPath;
    }

    public void setEditPath(String value) {
        this.editPath = value;
    }

    public EditorElement withEditPath(String value) {
        setEditPath(value);
        return this;
    }

    public EditorElement withEditPath(String value, String defaultValue) {
        setEditPath(value == null ? defaultValue : value);
        return this;
    }

    public Long getSeq() {
        return seq;
    }

    public void setSeq(Long value) {
        this.seq = value;
    }

    public EditorElement withSeq(Long value) {
        setSeq(value);
        return this;
    }

    public EditorElement withSeq(Long value, Long defaultValue) {
        setSeq(value == null ? defaultValue : value);
        return this;
    }

    public List<ShowValue> getFields() {
        if (fields == null) {
            fields = Lists.newArrayList();
        }
        return fields;
    }

    public void setFields(List<ShowValue> value) {
        this.fields = value;
    }

    public EditorElement withFields(List<ShowValue> value) {
        setFields(value);
        return this;
    }

    public EditorElement withFields(List<ShowValue> value, List<ShowValue> defaultValue) {
        setFields(value == null ? defaultValue : value);
        return this;
    }

    public EditorElement addFields(ShowValue value) {
        if (fields == null) {
            fields = Lists.newArrayList();
        }
        this.fields.add(value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "insertBelowPath":
                return getInsertBelowPath();
            case "deletePath":
                return getDeletePath();
            case "editPath":
                return getEditPath();
            case "seq":
                return getSeq();
            case "fields":
                return getFields();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof EditorElement)) return false;
        return signature().equals(((EditorElement) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean EditorElement has no signature defined, no hash code or equals possible.");
    }
}
